package fri.pipt.agent.deathrow;


import java.util.concurrent.TimeUnit;

import fri.pipt.agent.Membership;
import fri.pipt.agent.deathrow.state.StateMachine;
import fri.pipt.protocol.Message.Direction;

@Membership("samples")
public class Convict extends AgentAbstraction
{
	StateMachine stateMachine;
	
	VisualDebugger debugger;
	
	@Override
	public void update()
	{
		//Pred klicem update, se je že zgodil move, če je bil kakšen!
		stateMachine.update(getPosition());
		if(stateMachine.makeNextMove())
		{
			moveAgent(stateMachine.getDirection());
			stateMachine.moveCommandSend();
		}
		debugger.renderObservableArea();
		//moveAgent( stamp % 2 == 0 ? Direction.RIGHT : Direction.UP);
		
		//debugging: Do not delete 
//		moveAgent(dirs[step % dirs.length]);
	}

	@Override
	public void init()
	{
		System.out.println("ConvictInit");
		debugger = new VisualDebugger(this);
		stateMachine = new StateMachine(getWorld());
		stateMachine.init(); //Inicializira mašino! Postavi v začetno stanje!
	}

	@Override
	public void term()
	{
		System.out.println("Convict terminate");
		debugger.setVisible(false);
		debugger.dispose();
		debugger = null;
	}

	@Override
	public void incommingMessage(int from, byte[] msg)
	{
		
	}

	Direction[] dirs = {Direction.UP,Direction.RIGHT, Direction.RIGHT, Direction.DOWN, Direction.DOWN, Direction.LEFT, Direction.LEFT, Direction.UP};
	int step = 0;
	
	@Override
	public void agentMoved(Direction dir)
	{
		System.out.println("\tAgent moved: " + getPosition() + dir);
		stateMachine.agentMoved(getPosition());
		debugger.renderObservableArea();
		step++;
		
	}

	
}
